package com.qianguo.sender.order;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.sender.R;
import com.qianguo.sender.api.entity.SenderOrderInfo;
import com.qianguo.sender.api.entity.SenderOrderList;
import com.qianguo.sender.base.BaseFragment;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理中订单列表
 *
 * @author eva
 * @date 16/5/4
 */
@EFragment(R.layout.layout_swipe_list_view)
public class DoingOrderFragment extends BaseFragment {
    @ViewById(R.id.swipe_refresh_recycler_view)
    SwipeRefreshRecyclerView mRecyclerList;

    private DoingOrderAdapter mAdapter;
    private List<SenderOrderInfo> mDoingOrders = new ArrayList<>();

    private int mCurrentPage = 1;
    private static final int PER_PAGE = 10;

    @Override
    protected void initializeUI() {

        mAdapter = new DoingOrderAdapter(getActivity());
        mRecyclerList.setAdapter(mAdapter);
        mRecyclerList.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onLoadMore() {
                mCurrentPage++;
                getDoingOrders(mCurrentPage);
            }

            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mDoingOrders.clear();
                getDoingOrders(mCurrentPage);
            }
        });

        mRecyclerList.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                SenderOrderDetailActivity.jumpToOrderDetail(getActivity(), mDoingOrders.get(position).getOrdersn());
            }
        });
        getDoingOrders(mCurrentPage);
    }

    /**
     * 获取待接单订单列表
     */
    @Background
    void getDoingOrders(int page) {
        mApp.getApi().getDoingOrders(mApp.getUser().getToken(), page, PER_PAGE, new HttpRequestCallback<SenderOrderList>() {
            @Override
            public void onSuccess(int code, String msg, SenderOrderList data) {
                mDoingOrders.addAll(data.getList());
                refreshOrders();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    /**
     * 刷新列表
     */
    @UiThread
    void refreshOrders() {
        mRecyclerList.onCompleted();
        mRecyclerList.notifyDataSetChanged();
    }

    private class DoingOrderAdapter extends RecyclerView.Adapter<DoingOrderViewHolder> {

        private Context mContext;
        public DoingOrderAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public DoingOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_order_list, null);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
                    RecyclerView.LayoutParams.MATCH_PARENT,
                    RecyclerView.LayoutParams.WRAP_CONTENT);
            v.setLayoutParams(lp);
            return new DoingOrderViewHolder(v);
        }

        @Override
        public void onBindViewHolder(DoingOrderViewHolder holder, int position) {
            holder.onBind(mDoingOrders.get(position));
        }

        @Override
        public int getItemCount() {
            return mDoingOrders != null ? mDoingOrders.size() : 0;
        }
    }

    @Background
    void confirm(String ordersn) {
        mApp.getApi().confirmReceive(mApp.getUser().getToken(), ordersn, new HttpRequestCallback<SenderOrderInfo>() {
            @Override
            public void onSuccess(int code, String msg, SenderOrderInfo data) {
                mCurrentPage = 1;
                mDoingOrders.clear();
                getDoingOrders(mCurrentPage);
                showSnackBar(getActivity(), "确认送达成功");
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    private class DoingOrderViewHolder extends RecyclerView.ViewHolder {
        TextView tvConsignee;
        TextView tvReceivingTime;
        TextView tvReceivingAddress;
        TextView tvShopAddress;
        TextView tvDeliveryDistance;
        TextView tvDeliveryPrice;
        TextView tvIM;
        Button btnOK;

        private SenderOrderInfo item;

        public DoingOrderViewHolder(final View itemView) {
            super(itemView);
            tvConsignee = (TextView) itemView.findViewById(R.id.item_order_list_consignee_text_view);
            tvReceivingTime = (TextView) itemView.findViewById(R.id.item_order_list_receiving_time_text_view);
            tvReceivingAddress = (TextView) itemView.findViewById(R.id.item_order_list_receiving_address_text_view);
            tvShopAddress = (TextView) itemView.findViewById(R.id.item_order_list_shop_address_text_view);
            tvDeliveryDistance = (TextView) itemView.findViewById(R.id.item_order_list_delivery_distance_text_view);
            tvDeliveryPrice = (TextView) itemView.findViewById(R.id.item_order_list_delivery_price_text_view);
            btnOK = (Button) itemView.findViewById(R.id.item_order_list_action_button);
            btnOK.setText(getString(R.string.sender_order_action_confirm_arrived));
            btnOK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirm(item.getOrdersn());
                }
            });
            // TODO: 16/5/5 IM
        }

        void onBind(SenderOrderInfo order) {
            this.item = order;
            tvConsignee.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_consignee), order.getReceiverName()));
            tvReceivingAddress.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_receiving_address), order.getReceiverAddress()));
            tvReceivingTime.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_receiving_time), order.getDeliveryTime()));
            tvShopAddress.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_shop_address), order.getStoreAddress()));
            tvDeliveryDistance.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_delivery_distance), order.getDeliveryDistance()));
            tvDeliveryPrice.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_delivery_price), Integer.valueOf(order.getDeliveryPrice())));
        }
    }

}
